from rest_framework import serializers

from xyz.models import Label, Question, Article, User


class Questionserializer(serializers.ModelSerializer):
    labels = serializers.StringRelatedField(many=True)

    class Meta:
        model = Question
        fields = ('id', 'createtime', 'labels', 'reply', 'replyname', 'replytime', 'title', 'unuseful_count', 'useful_count', 'user', 'visits')
        # fields = '__all__'


class USerserializer(serializers.ModelSerializer):
    # articles = serializers.StringRelatedField(many=True)

    class Meta:
        model = User
        fields = ("id", "username", "avatar", "articles", "fans")
        depth = 1


class Articlesserializer(serializers.ModelSerializer):
    user = USerserializer()

    class Meta:
        model = Article
        fields = '__all__'
        # depth = 2


class Labelserializer(serializers.ModelSerializer):

    questions = Questionserializer(many=True)
    articles = Articlesserializer(many=True)

    class Meta:
        model = Label
        fields = ('id', 'questions', 'label_name', 'desc', 'baike_url', 'label_icon', 'users','articles')
        # depth = 4